<template>
  <div class="hello">
    <el-container>
      <el-header style="height: 180px;width: 540px;margin: 20px auto;">
        <div class="title-container">
          <h3 class="title">高等教育作业管理系统</h3>
          <h3 class="title">领导注册</h3>
        </div>
      </el-header>
      <el-main style="margin: 0 auto;width: 540px;">
        <!--  领导注册 -->
        <el-form :model="leaderForm" status-icon :rules="rules" ref="leaderForm" class="demo-ruleForm">
          <el-form-item label="姓名" prop="leaderName">
            <el-input type="text" placeholder="请输入姓名" v-model="leaderForm.leaderName"></el-input>
          </el-form-item>
          <el-form-item label="工号" prop="leaderNum">
            <el-input type="text" placeholder="请输入账号" v-model="leaderForm.leaderNum"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="leaderPassword">
            <el-input type="password" placeholder="请输入密码" v-model="leaderForm.leaderPassword"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('leaderForm')">立即注册</el-button>
            <el-button @click="resetForm('leaderForm')">重置</el-button>
            <router-link to="/leaderLogin">
              <el-button>前往登录</el-button>
            </router-link>
          </el-form-item>
        </el-form>
      </el-main>
      <el-footer style="width: 540px;margin: 10px auto;">
        <div>
          <router-link to="/adminLogin">
            <el-button type="primary" size="mini">管理员登录</el-button>
          </router-link>
          <router-link to="/leaderLogin">
            <el-button type="primary" size="mini">领导登录</el-button>
          </router-link>
          <router-link to="/teacherLogin">
            <el-button type="primary" size="mini">教师登录</el-button>
          </router-link>
          <router-link to="/teacherSecretaryLogin">
            <el-button type="primary" size="mini">教学秘书登录</el-button>
          </router-link>
          <router-link to="/studentLogin">
            <el-button type="primary" size="mini">学生登录</el-button>
          </router-link>
        </div>
      </el-footer>
    </el-container>
  </div>
</template>

<script>
export default {
  name: 'leaderLogin',
  data () {
    return {
      leaderForm: {
        leaderNum: '',
        leaderName: '',
        leaderPassword: ''
      },
      rules: {
        leaderNum: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ],
        leaderPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
        leaderName: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ]
      },
      loading: false,
      passwordType: 'leaderPassword'
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('http://localhost:8888/leaderRegister', this.leaderForm).then(res => {
            if (res.data.flag) {
              this.$message({
                message: '恭喜你，注册成功啦',
                type: 'success'
              })
              this.loading = true
              this.leaderForm.leaderName = ''
              this.leaderForm.leaderNum = ''
              this.leaderForm.leaderPassword = ''
              this.jump(res.data.data)
            } else if (res.data.data === 'err') {
              this.loading = true
              this.$message({
                message: '警告哦，此工号已注册',
                type: 'warning'
              })
              this.leaderForm.leaderNum = ''
              this.leaderForm.leaderPassword = ''
            }
          }).catch(() => {
            this.$message.error('很遗憾哦，注册失败啦')
          })
        } else {
          this.loading = true
          this.$message({
            message: '警告哦，请注意填写格式',
            type: 'warning'
          })
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    jump (data) {
      this.$confirm('您已注册成功，是否立即登录?\', \'提示\'', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        this.$message({
          type: 'success',
          message: '正在前往登录页登录...'
        })
        this.$router.push({path: '/leaderLogin', query: { leaderInfo: data }})
      }).catch(() => {
        this.loading = false
        this.$message({
          type: 'info',
          message: '暂不前往登录'
        })
      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  .el-header, .el-footer {
    background-color: #c6d1dc;
    color: #333;
    text-align: center;
    line-height: 60px;
  }

  .el-aside {
    background-color: #D3DCE6;
    color: #333;
    text-align: center;
    line-height: 200px;
  }

  .el-main {
    background-color: #E9EEF3;
    color: #333;
    text-align: center;
    line-height: 160px;
  }

  body > .el-container {
    margin-bottom: 40px;
  }

  .el-container:nth-child(5) .el-aside,
  .el-container:nth-child(6) .el-aside {
    line-height: 260px;
  }

  .el-container:nth-child(7) .el-aside {
    line-height: 320px;
  }
  .login-form{
    width: 500px;
  }
</style>
